package auctionhaus

import org.codehaus.groovy.grails.plugins.springsecurity.SpringSecurityUtils

class CustomerCreateService {

    def springSecurityService

    def createCustomer(params) {

        Customer customerInstance = new Customer(params)
        customerInstance.enabled = true
        customerInstance.save(flush: true)

        try{
            Role userRole = Role.findByAuthority("ROLE_USER")
        
            if(!customerInstance.hasErrors())
            {
                CustomerRole.create customerInstance, userRole, true
                if(!springSecurityService.isLoggedIn())
                    springSecurityService.reauthenticate customerInstance.email
            }
        }
        catch(groovy.lang.MissingMethodException e)
        {
            //okay, probably just unit testing
        }

        return customerInstance
    }
}
